package com.edevp.common.core.util.date;

import com.edevp.common.core.util.CommonUtil;
import lombok.SneakyThrows;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间操作类
 *
 * @author Blueeyedboy
 * @create 2019-04-08 4:48 PM
 **/
public class DateUtil {
	/**
	 * 获取系统当前时间
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 14:18
	 **/
	public static Date getDate(){
		return new Date(System.currentTimeMillis());
	}


	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
	 *
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrLong(Date dateDate) {
		if(!CommonUtil.isNotNull(dateDate)){
			return "";
		}
		DateFormat mediumDateFormat =
				DateFormat.getDateTimeInstance(
						DateFormat.MEDIUM,
						DateFormat.MEDIUM);
		//System.out.println(dateDate.toGMTString());;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 获取当前日期字符串2017-12-12
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 14:25
	 **/
	public static String getDateStr(Date currentTime){
		if(currentTime == null){
			currentTime = getDate();
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取当前日期字符串2017-12-12
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 14:25
	 **/
	public static String getDateStr(){

		return getDateStr(getDate());
	}



	/**
	 * 获取当前日期时间字符串2017-12-12 12:12:12
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 14:26
	 **/
	public static String getDateTimeStr(Date currentTime){
		if(currentTime == null){
			//currentTime = getDate();
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
	 *
	 * @param strDate
	 * @return
	 */
	public static Date strToDateTime(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 *
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 获取当前日期时间字符串2017-12-12 12:12:12
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 14:26
	 **/
	public static String getDateTimeStr(){

		Date  currentTime = getDate();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
	/**
	 * 获取当前日期时间字符串毫秒级别2017-12-12 12:12:12:111
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 14:26
	 **/
	public static String getDateTimeLongStr(){

		Date  currentTime = getDate();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取指定日期格式当前日期的字符型日期
	 *
	 * @param p_format
	 *            日期格式 格式1:"yyyy-MM-dd" 格式2:"yyyy-MM-dd hh:mm:ss EE" 格式3:"yyyy年MM月dd日 hh:mm:ss EE" 说明: 年-月-日 时:分:秒 星期 注意MM/mm大小写
	 * @return String 当前时间字符串
	 * @author zhuqx
	 * @Date: 2006-10-31
	 */
	public static String getDateTimeByFormat(String p_format) {
		Date d = getDate();
		SimpleDateFormat sdf = new SimpleDateFormat(p_format);
		String dateStr = sdf.format(d);
		return dateStr;
	}

	/**
	 *  计算距离生日还有多少天
	 * @param addtime：生日日期
	 */
	public static int getBirthDay(String addtime) {
		int days = 0;
		try {
			SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
			String clidate = addtime;
			Calendar cToday = Calendar.getInstance(); // 存今天
			Calendar cBirth = Calendar.getInstance(); // 存生日
			cBirth.setTime(myFormatter.parse(clidate)); // 设置生日
			cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年
			if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
				// 生日已经过了，要算明年的了
				days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
				days += cBirth.get(Calendar.DAY_OF_YEAR);
			} else {
				// 生日还没过
				days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}

	@SneakyThrows
	public static int getDays(String dateStr1){
		if(CommonUtil.isNull(dateStr1)){
			return 0;
		}
		Date date1 = getDate();
		Date date2 = strToDate(dateStr1);

		return getDays(date1,date2);
	}

	@SneakyThrows
	public static int getDays(String dateStr1, String dateStr2){
		Date date1 = strToDate(dateStr1);
		Date date2 = strToDate(dateStr2);
		return getDays(date1,date2);
	}

	@SneakyThrows
	public static int getDays(Date date1, Date date2){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		date1=sdf.parse(sdf.format(date1));
		date2=sdf.parse(sdf.format(date2));
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}
}
